﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace huaguwang
{
    public class D_T_MenterGroup_Log_Exp : D_T_MenterGroup_Log
    {
        #region 扩展方法
        public int Find(string UserName, string MName)
        {
            //            select a.id,b.realname,a.adddatetime,a.modulename,a.functionname from dbo.T_MenterGroup_Log a inner join 
            //t_adminuser b on a.userid=b.id
            //where a.flag=1

            var strSql = ExpandMSSQL.GetPageSQL(
                    "dbo.T_MenterGroup_Log a inner join t_adminuser b on a.userid=b.id",
                    " a.flag=1 and b.realname like '%'+@name+'%' and a.modulename like '%'+@MName+'%' "
            );
            var items = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strSql,
                new System.Data.SqlClient.SqlParameter("@name", SqlDbType.NVarChar) { Value = UserName },
                new System.Data.SqlClient.SqlParameter("@mname", SqlDbType.NVarChar) { Value = MName }
                );
            return items.Exp_IntTryParse();
        }
        public DataTable Find(int begin, int page, string UserName, string MName)
        {
            //            select a.id,b.realname,a.adddatetime,a.modulename,a.functionname from dbo.T_MenterGroup_Log a inner join 
            //t_adminuser b on a.userid=b.id
            //where a.flag=1

            var strSql = ExpandMSSQL.GetPageSQL(
                    begin,
                    page,
                    "a.id,b.realname,a.adddatetime,a.modulename,a.functionname,a.IP",
                    "dbo.T_MenterGroup_Log a inner join t_adminuser b on a.userid=b.id",
                    " a.id ",
                    " a.flag=1 and b.realname like '%'+@name+'%' and a.modulename like '%'+@MName+'%' ",
                    " a.adddatetime desc  ");
            var items = MSSQLHelper.SQLConType.C1.Exp_Query(strSql,
                new System.Data.SqlClient.SqlParameter("@name", SqlDbType.NVarChar) { Value = UserName },
                new System.Data.SqlClient.SqlParameter("@mname", SqlDbType.NVarChar) { Value = MName }
                );
            return items.Tables[0];
        }


        public DataTable GetList(int StartIndex, int TakeIndex, string ModelName, string UserName, string BeginDT, string EndDT, out int Count)
        {
            var con = " t2.Flag = 1 and t1.UserID = t2.id ";
            var tab = "T_MenterGroup_Log t1,T_AdminUser t2";
            if(ModelName != "")
            {
                con += " and t1.ModuleName like '%'+@ModelName+'%' ";
            }
            if(UserName != "")
            {
                con += " and t2.URL like '%'+@URL+'%' ";
            }
            if(BeginDT != "")
            {
                con += " and t1.AddDateTime > @BeginDT ";
            }
            if(EndDT != "")
            {
                con += " and t1.AddDateTime < @EndDT ";
            }

            SqlParameter[] Pams = { 
                                      new SqlParameter("@ModelName", ModelName), 
                                      new SqlParameter("@URL",UserName),
                                      new SqlParameter("@BeginDT",BeginDT),
                                      new SqlParameter("@EndDT",EndDT),
                                  };

            var countsql = huaguwang.ExpandMSSQL.GetPageSQL(tab, con);
            Count = MSSQLHelper.SQLConType.C1.Exp_GetSingle(countsql, Pams).Exp_IntTryParse();

            var sql = huaguwang.ExpandMSSQL.GetPageSQL(
                StartIndex,
                TakeIndex,
                "t1.*, t2.URL ",
                tab,
                "t1.ID",
                con,
                "t1.ID Desc"
                );

            var dsset = MSSQLHelper.SQLConType.C1.Exp_Query(sql, Pams);

            return dsset.Tables[0];
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int DelExp(int id)
        {
            return huaguwang.MSSQLHelper.SQLConType.C1.Exp_UpdateFlag("T_MenterGroup_Log", id);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public int DelExp(List<int> list)
        {
            return huaguwang.MSSQLHelper.SQLConType.C1.Exp_UpdateFlag("T_MenterGroup_Log", list);
        }
        #endregion
    }
}
